<?php

namespace app\index\controller;

use tian\Controller;
use tian\Request;
use tian\Session;
use think\facade\Db;

class Base extends Controller
{
    protected $userInfo;
    protected $role_id;
    protected $roleInfo;

    public function _before_action(): void
    {
        $request = Request::instance();

        $isLogin = Session::has('id');
        if ($isLogin == false) {
            redirect('/index/index/login');
        }

        $isRole = Session::has('uid');
        if ($isRole == false && strtolower($request->controller()) != 'role') {
            redirect('/index/role/index');
        }

        $this->getUserInfo();

        $this->getRoleInfo();
    }

    public function getUserInfo()
    {
        $user_id = Session::get('id');

        //获取用户信息
        $userInfo = Db::name('userinfo')->where('id', $user_id)->find();
        $this->userInfo = $userInfo;
        $this->assign('userInfo', $userInfo);
    }

    public function getRoleInfo()
    {
        $this->role_id = Session::get('uid');

        //获取用户信息
        $roleInfo = model('Role')->getInfo($this->role_id);
        if (!empty($roleInfo)) {
            //区域信息
            $roleInfo['map'] = model('MapArea')->getInfo($roleInfo['nowmid']);
            //怪物信息
            $roleInfo['monster'] = $this->getMonster($roleInfo['map']);
            //npc信息
            $roleInfo['mnpc_list'] = $this->getNpc($roleInfo['map']);
            //出战宠物
            $roleInfo['pets'] = [];
            if ($roleInfo['cw'] != 0) {
                $roleInfo['pets'] = model('RolePets')->getInfo($roleInfo['cw']);
            }
        }

        $this->roleInfo = $roleInfo;
        $this->assign('roleInfo', $roleInfo);
    }

    protected function getMonster($infoMap)
    {
        $role_id = Session::get('uid');
        $mgidAry = explode(",", $infoMap['mgid']);
        $midboss = $infoMap['midboss'];

        $time = time();
        $nowdate = date('Y-m-d H:i:s');
        $second = floor((strtotime($nowdate) - strtotime($infoMap['mgtime'])) % 86400);
        if ($second > $infoMap['ms'] && $infoMap['mgid'] != '') {
            foreach ($mgidAry as $val) {
                $ary = explode("|", $val);
                $infoMonster = model('Monster')->getInfo($ary[0]);
                $sjexp = mt_rand(6, 8) + 0.5;
                $gexp = round($infoMonster['glv'] * $sjexp, 0);

                $map['mid'] = $infoMap['mid'];
                $map['gyid'] = $ary[0];
                $cxallguaiwu = model('MapMonster')->countNum($map);
                $num = $ary[1] - $cxallguaiwu;

                for ($n = 0; $n < $num; $n++) {
                    $add['sid'] = 0;
                    $add['mid'] = $infoMap['mid'];
                    $add['type'] = 1;
                    $add['gyid'] = $infoMonster['id'];
                    $add['gname'] = $infoMonster['gname'];
                    $add['ginfo'] = $infoMonster['ginfo'];
                    $add['glv'] = $infoMonster['glv'];
                    $add['ghp'] = $infoMonster['ghp'];
                    $add['gmaxhp'] = $infoMonster['ghp'];
                    $add['ggj'] = $infoMonster['ggj'];
                    $add['gfy'] = $infoMonster['gfy'];
                    $add['gbj'] = $infoMonster['gbj'];
                    $add['gxx'] = $infoMonster['gxx'];
                    $add['gsd'] = $infoMonster['gsd'];
                    $add['gexp'] = $gexp;
                    $add['addtime'] = $time;
                    $getId = model('MapMonster')->add($add);
                }
            }
        }

        if($midboss != 0){
            $infoMapMonsterBoss = model('MapMonster')->getWhereInfo(['gyid'=>$midboss]);
            if(empty($infoMapMonsterBoss)){
                $infoBoss = model('Boss')->getInfo($midboss);
                if(!empty($infoBoss)){
                    $secondBoss = floor(strtotime($infoBoss['bosstime']) + $infoBoss['bs']);
                    $rate1 = mt_rand(1, 10);
                    $rate2 = mt_rand(5, 15);
                    //满足刷新条件 + 遇到领主概率
                    if($time > $secondBoss && $rate1 == $rate2){
                        $sjexp = mt_rand(8, 15) + 0.5;
                        $gexp = round($infoBoss['bosslv'] * $sjexp, 0);

                        $add['sid'] = 0;
                        $add['mid'] = $infoMap['mid'];
                        $add['type'] = 2;
                        $add['gyid'] = $infoBoss['bossid'];
                        $add['gname'] = $infoBoss['bossname'];
                        $add['ginfo'] = $infoBoss['bossinfo'];
                        $add['glv'] = $infoBoss['bosslv'];
                        $add['ghp'] = $infoBoss['bosshp'];
                        $add['gmaxhp'] = $infoBoss['bossmaxhp'];
                        $add['ggj'] = $infoBoss['bossgj'];
                        $add['gfy'] = $infoBoss['bossfy'];
                        $add['gbj'] = $infoBoss['bossbj'];
                        $add['gxx'] = $infoBoss['bossxx'];
                        $add['gsd'] = $infoBoss['sd'];
                        $add['gexp'] = $gexp;
                        $add['addtime'] = $time;
                        model('MapMonster')->add($add);

                        $temp = [
                            'bossid' => $infoBoss['bossid'],
                            'bosstime' => date('Y-m-d H:i:s'),
                        ];
                        model('Boss')->saved($temp);
                    }
                }
            }else{
                $typetime = $infoMapMonsterBoss['havetime'] + 10 * 60;
                if($time > $typetime){
                    //更新领主归属为无主
                    $tempBoss = [
                        'id' => $infoMapMonsterBoss['id'],
                        'sid' => 0,
                        'havetime' => 0,
                        'addtime' => $time,
                    ];
                    model('MapMonster')->saved($tempBoss);
                }
            }
        }

        $map1 = [
            ['mid','=',$infoMap['mid']],
            ['sid','=',$role_id]
        ];
        $map2 = [
            ['mid','=',$infoMap['mid']],
            ['sid','=',0]
         ];
        $list = model('MapMonster')->whereOr([$map1, $map2])->order('type desc,id asc')->select();

        return $list;
    }

    protected function getNpc($infoMap)
    {
        $mnpcAry = explode(",", $infoMap['mnpc']);

        $list = [];
        if (!empty($mnpcAry) && $infoMap['mnpc'] != '') {
            foreach ($mnpcAry as $k => $item) {
                $infoNpc = model('Npc')->getInfo($item);

                $str = '';
                if ($infoNpc['nickname'] != '') {
                    $str = '<' . $infoNpc['nickname'] . '>';
                }

                $list[$k]['id'] = $item;
                $list[$k]['name'] = '[Lv:' . $infoNpc['nlv'] . '][' . $infoNpc['nsex'] . ']-' . $infoNpc['nname'] . $str;
            }
        }

        return $list;
    }

    /**
     * 解析加密字符串
     * @param string $str 加密字符串
     * @param int $uid 过滤id
     * @return int|string 返回id或字符串
     */
    protected function strAnalysis(string $str, int $uid)
    {
        $str = substr($str,0,-4);
        $str = str_base64_decode($str);
        $strAry = explode('|', $str);
        if (!in_array($uid, $strAry)) {
            redirect('/index/home/index');
        }

        $strAry = array_flip($strAry);
        unset($strAry[$uid]);
        $strAry = array_flip($strAry);

        return $strAry[0];
    }

}
